{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "bfea208e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "各系数为:[5.71421731e-02 9.61723492e+01 1.13452022e+02 5.61326459e-02\n",
      " 1.97874093e+00]\n",
      "常数项系数k0为:-208.4200407997355\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table class=\"simpletable\">\n",
       "<caption>OLS Regression Results</caption>\n",
       "<tr>\n",
       "  <th>Dep. Variable:</th>          <td>客户价值</td>       <th>  R-squared:         </th> <td>   0.571</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Model:</th>                   <td>OLS</td>       <th>  Adj. R-squared:    </th> <td>   0.553</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Method:</th>             <td>Least Squares</td>  <th>  F-statistic:       </th> <td>   32.44</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Date:</th>             <td>Thu, 09 Oct 2025</td> <th>  Prob (F-statistic):</th> <td>6.41e-21</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Time:</th>                 <td>08:10:00</td>     <th>  Log-Likelihood:    </th> <td> -843.50</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>No. Observations:</th>      <td>   128</td>      <th>  AIC:               </th> <td>   1699.</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Df Residuals:</th>          <td>   122</td>      <th>  BIC:               </th> <td>   1716.</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Df Model:</th>              <td>     5</td>      <th>                     </th>     <td> </td>   \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Covariance Type:</th>      <td>nonrobust</td>    <th>                     </th>     <td> </td>   \n",
       "</tr>\n",
       "</table>\n",
       "<table class=\"simpletable\">\n",
       "<tr>\n",
       "     <td></td>       <th>coef</th>     <th>std err</th>      <th>t</th>      <th>P>|t|</th>  <th>[0.025</th>    <th>0.975]</th>  \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>const</th>  <td> -208.4200</td> <td>  163.810</td> <td>   -1.272</td> <td> 0.206</td> <td> -532.699</td> <td>  115.859</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>历史贷款金额</th> <td>    0.0571</td> <td>    0.010</td> <td>    5.945</td> <td> 0.000</td> <td>    0.038</td> <td>    0.076</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>贷款次数</th>   <td>   96.1723</td> <td>   25.962</td> <td>    3.704</td> <td> 0.000</td> <td>   44.778</td> <td>  147.567</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>学历</th>     <td>  113.4520</td> <td>   37.909</td> <td>    2.993</td> <td> 0.003</td> <td>   38.406</td> <td>  188.498</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>月收入</th>    <td>    0.0561</td> <td>    0.019</td> <td>    2.941</td> <td> 0.004</td> <td>    0.018</td> <td>    0.094</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>性别</th>     <td>    1.9787</td> <td>   32.286</td> <td>    0.061</td> <td> 0.951</td> <td>  -61.934</td> <td>   65.891</td>\n",
       "</tr>\n",
       "</table>\n",
       "<table class=\"simpletable\">\n",
       "<tr>\n",
       "  <th>Omnibus:</th>       <td> 1.597</td> <th>  Durbin-Watson:     </th> <td>   2.155</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Prob(Omnibus):</th> <td> 0.450</td> <th>  Jarque-Bera (JB):  </th> <td>   1.538</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Skew:</th>          <td> 0.264</td> <th>  Prob(JB):          </th> <td>   0.464</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Kurtosis:</th>      <td> 2.900</td> <th>  Cond. No.          </th> <td>1.28e+05</td>\n",
       "</tr>\n",
       "</table><br/><br/>Notes:<br/>[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.<br/>[2] The condition number is large, 1.28e+05. This might indicate that there are<br/>strong multicollinearity or other numerical problems."
      ],
      "text/latex": [
       "\\begin{center}\n",
       "\\begin{tabular}{lclc}\n",
       "\\toprule\n",
       "\\textbf{Dep. Variable:}    &       客户价值       & \\textbf{  R-squared:         } &     0.571   \\\\\n",
       "\\textbf{Model:}            &       OLS        & \\textbf{  Adj. R-squared:    } &     0.553   \\\\\n",
       "\\textbf{Method:}           &  Least Squares   & \\textbf{  F-statistic:       } &     32.44   \\\\\n",
       "\\textbf{Date:}             & Thu, 09 Oct 2025 & \\textbf{  Prob (F-statistic):} &  6.41e-21   \\\\\n",
       "\\textbf{Time:}             &     08:10:00     & \\textbf{  Log-Likelihood:    } &   -843.50   \\\\\n",
       "\\textbf{No. Observations:} &         128      & \\textbf{  AIC:               } &     1699.   \\\\\n",
       "\\textbf{Df Residuals:}     &         122      & \\textbf{  BIC:               } &     1716.   \\\\\n",
       "\\textbf{Df Model:}         &           5      & \\textbf{                     } &             \\\\\n",
       "\\textbf{Covariance Type:}  &    nonrobust     & \\textbf{                     } &             \\\\\n",
       "\\bottomrule\n",
       "\\end{tabular}\n",
       "\\begin{tabular}{lcccccc}\n",
       "                & \\textbf{coef} & \\textbf{std err} & \\textbf{t} & \\textbf{P$> |$t$|$} & \\textbf{[0.025} & \\textbf{0.975]}  \\\\\n",
       "\\midrule\n",
       "\\textbf{const}  &    -208.4200  &      163.810     &    -1.272  &         0.206        &     -532.699    &      115.859     \\\\\n",
       "\\textbf{历史贷款金额} &       0.0571  &        0.010     &     5.945  &         0.000        &        0.038    &        0.076     \\\\\n",
       "\\textbf{贷款次数}   &      96.1723  &       25.962     &     3.704  &         0.000        &       44.778    &      147.567     \\\\\n",
       "\\textbf{学历}     &     113.4520  &       37.909     &     2.993  &         0.003        &       38.406    &      188.498     \\\\\n",
       "\\textbf{月收入}    &       0.0561  &        0.019     &     2.941  &         0.004        &        0.018    &        0.094     \\\\\n",
       "\\textbf{性别}     &       1.9787  &       32.286     &     0.061  &         0.951        &      -61.934    &       65.891     \\\\\n",
       "\\bottomrule\n",
       "\\end{tabular}\n",
       "\\begin{tabular}{lclc}\n",
       "\\textbf{Omnibus:}       &  1.597 & \\textbf{  Durbin-Watson:     } &    2.155  \\\\\n",
       "\\textbf{Prob(Omnibus):} &  0.450 & \\textbf{  Jarque-Bera (JB):  } &    1.538  \\\\\n",
       "\\textbf{Skew:}          &  0.264 & \\textbf{  Prob(JB):          } &    0.464  \\\\\n",
       "\\textbf{Kurtosis:}      &  2.900 & \\textbf{  Cond. No.          } & 1.28e+05  \\\\\n",
       "\\bottomrule\n",
       "\\end{tabular}\n",
       "%\\caption{OLS Regression Results}\n",
       "\\end{center}\n",
       "\n",
       "Notes: \\newline\n",
       " [1] Standard Errors assume that the covariance matrix of the errors is correctly specified. \\newline\n",
       " [2] The condition number is large, 1.28e+05. This might indicate that there are \\newline\n",
       " strong multicollinearity or other numerical problems."
      ],
      "text/plain": [
       "<class 'statsmodels.iolib.summary.Summary'>\n",
       "\"\"\"\n",
       "                            OLS Regression Results                            \n",
       "==============================================================================\n",
       "Dep. Variable:                   客户价值   R-squared:                       0.571\n",
       "Model:                            OLS   Adj. R-squared:                  0.553\n",
       "Method:                 Least Squares   F-statistic:                     32.44\n",
       "Date:                Thu, 09 Oct 2025   Prob (F-statistic):           6.41e-21\n",
       "Time:                        08:10:00   Log-Likelihood:                -843.50\n",
       "No. Observations:                 128   AIC:                             1699.\n",
       "Df Residuals:                     122   BIC:                             1716.\n",
       "Df Model:                           5                                         \n",
       "Covariance Type:            nonrobust                                         \n",
       "==============================================================================\n",
       "                 coef    std err          t      P>|t|      [0.025      0.975]\n",
       "------------------------------------------------------------------------------\n",
       "const       -208.4200    163.810     -1.272      0.206    -532.699     115.859\n",
       "历史贷款金额         0.0571      0.010      5.945      0.000       0.038       0.076\n",
       "贷款次数          96.1723     25.962      3.704      0.000      44.778     147.567\n",
       "学历           113.4520     37.909      2.993      0.003      38.406     188.498\n",
       "月收入            0.0561      0.019      2.941      0.004       0.018       0.094\n",
       "性别             1.9787     32.286      0.061      0.951     -61.934      65.891\n",
       "==============================================================================\n",
       "Omnibus:                        1.597   Durbin-Watson:                   2.155\n",
       "Prob(Omnibus):                  0.450   Jarque-Bera (JB):                1.538\n",
       "Skew:                           0.264   Prob(JB):                        0.464\n",
       "Kurtosis:                       2.900   Cond. No.                     1.28e+05\n",
       "==============================================================================\n",
       "\n",
       "Notes:\n",
       "[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n",
       "[2] The condition number is large, 1.28e+05. This might indicate that there are\n",
       "strong multicollinearity or other numerical problems.\n",
       "\"\"\""
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "df = pd.read_excel(\"D:\\贺之恒的python\\pppython\\python商业数据分析\\@Python大数据分析与机器学习商业案例实战\\第3章 线性回归模型\\源代码汇总_PyCharm格式\\客户价值数据表.xlsx\")\n",
    "df.head()  # 显示前5行数据\n",
    "\n",
    "X = df[['历史贷款金额', '贷款次数', '学历', '月收入', '性别']]\n",
    "Y = df['客户价值']\n",
    "\n",
    "# 3.模型搭建\n",
    "from sklearn.linear_model import LinearRegression\n",
    "regr = LinearRegression()\n",
    "regr.fit(X,Y)\n",
    "\n",
    "# 4.线性回归方程构造\n",
    "regr.coef_\n",
    "\n",
    "print('各系数为:' + str(regr.coef_))\n",
    "print('常数项系数k0为:' + str(regr.intercept_))\n",
    "\n",
    "# 5.模型评估\n",
    "import statsmodels.api as sm  # 引入线性回归模型评估相关库\n",
    "X2 = sm.add_constant(X)\n",
    "est = sm.OLS(Y, X2).fit()\n",
    "est.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "04c9e337",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "==================================================\n",
      "模型1: 多元线性回归模型\n",
      "==================================================\n",
      "各系数为:[104.24640708  73.05600375  61.3239286   62.38748327   7.06888361]\n",
      "常数项系数为:1321.1753706505797\n",
      "R²得分: 0.5803\n",
      "均方误差: 24535.0294\n",
      "\n",
      "Statsmodels详细回归结果:\n",
      "                            OLS Regression Results                            \n",
      "==============================================================================\n",
      "Dep. Variable:                   客户价值   R-squared:                       0.571\n",
      "Model:                            OLS   Adj. R-squared:                  0.553\n",
      "Method:                 Least Squares   F-statistic:                     32.44\n",
      "Date:                Thu, 09 Oct 2025   Prob (F-statistic):           6.41e-21\n",
      "Time:                        08:12:36   Log-Likelihood:                -843.50\n",
      "No. Observations:                 128   AIC:                             1699.\n",
      "Df Residuals:                     122   BIC:                             1716.\n",
      "Df Model:                           5                                         \n",
      "Covariance Type:            nonrobust                                         \n",
      "==============================================================================\n",
      "                 coef    std err          t      P>|t|      [0.025      0.975]\n",
      "------------------------------------------------------------------------------\n",
      "const       -208.4200    163.810     -1.272      0.206    -532.699     115.859\n",
      "历史贷款金额         0.0571      0.010      5.945      0.000       0.038       0.076\n",
      "贷款次数          96.1723     25.962      3.704      0.000      44.778     147.567\n",
      "学历           113.4520     37.909      2.993      0.003      38.406     188.498\n",
      "月收入            0.0561      0.019      2.941      0.004       0.018       0.094\n",
      "性别             1.9787     32.286      0.061      0.951     -61.934      65.891\n",
      "==============================================================================\n",
      "Omnibus:                        1.597   Durbin-Watson:                   2.155\n",
      "Prob(Omnibus):                  0.450   Jarque-Bera (JB):                1.538\n",
      "Skew:                           0.264   Prob(JB):                        0.464\n",
      "Kurtosis:                       2.900   Cond. No.                     1.28e+05\n",
      "==============================================================================\n",
      "\n",
      "Notes:\n",
      "[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n",
      "[2] The condition number is large, 1.28e+05. This might indicate that there are\n",
      "strong multicollinearity or other numerical problems.\n",
      "\n",
      "==================================================\n",
      "模型2: 随机森林回归模型\n",
      "==================================================\n",
      "特征重要性:\n",
      "历史贷款金额: 0.3875\n",
      "贷款次数: 0.1177\n",
      "学历: 0.1067\n",
      "月收入: 0.3576\n",
      "性别: 0.0305\n",
      "R²得分: 0.6330\n",
      "均方误差: 21453.5251\n",
      "\n",
      "==================================================\n",
      "模型3: 支持向量机回归模型\n",
      "==================================================\n",
      "R²得分: 0.0241\n",
      "均方误差: 57044.0892\n",
      "\n",
      "==================================================\n",
      "模型性能对比\n",
      "==================================================\n",
      "        模型      R²得分          均方误差\n",
      "0   多元线性回归  0.580255  24535.029418\n",
      "1   随机森林回归  0.632973  21453.525150\n",
      "2  支持向量机回归  0.024091  57044.089211\n",
      "\n",
      "==================================================\n",
      "附加模型: 正则化线性回归\n",
      "==================================================\n",
      "岭回归 R²得分: 0.5824\n",
      "Lasso回归 R²得分: 0.5806\n",
      "Lasso回归系数:[104.16712019  72.9920653   61.28721492  62.35811754   6.97110825]\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn.linear_model import LinearRegression, Ridge, Lasso\n",
    "from sklearn.ensemble import RandomForestRegressor\n",
    "from sklearn.svm import SVR\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import r2_score, mean_squared_error\n",
    "import statsmodels.api as sm\n",
    "\n",
    "# 读取数据\n",
    "df = pd.read_excel(\"D:\\贺之恒的python\\pppython\\python商业数据分析\\@Python大数据分析与机器学习商业案例实战\\第3章 线性回归模型\\源代码汇总_PyCharm格式\\客户价值数据表.xlsx\")\n",
    "df.head()  # 显示前5行数据\n",
    "\n",
    "X = df[['历史贷款金额', '贷款次数', '学历', '月收入', '性别']]\n",
    "Y = df['客户价值']\n",
    "\n",
    "# 数据预处理：标准化\n",
    "scaler = StandardScaler()\n",
    "X_scaled = scaler.fit_transform(X)\n",
    "\n",
    "# 划分训练集和测试集\n",
    "X_train, X_test, y_train, y_test = train_test_split(X_scaled, Y, test_size=0.2, random_state=42)\n",
    "\n",
    "print(\"=\" * 50)\n",
    "print(\"模型1: 多元线性回归模型\")\n",
    "print(\"=\" * 50)\n",
    "\n",
    "# 模型1: 多元线性回归\n",
    "lr_model = LinearRegression()\n",
    "lr_model.fit(X_train, y_train)\n",
    "\n",
    "# 预测\n",
    "y_pred_lr = lr_model.predict(X_test)\n",
    "\n",
    "# 评估\n",
    "r2_lr = r2_score(y_test, y_pred_lr)\n",
    "mse_lr = mean_squared_error(y_test, y_pred_lr)\n",
    "\n",
    "print('各系数为:' + str(lr_model.coef_))\n",
    "print('常数项系数为:' + str(lr_model.intercept_))\n",
    "print(f'R²得分: {r2_lr:.4f}')\n",
    "print(f'均方误差: {mse_lr:.4f}')\n",
    "\n",
    "# 使用statsmodels进行详细分析\n",
    "X_with_const = sm.add_constant(X)\n",
    "est = sm.OLS(Y, X_with_const).fit()\n",
    "print(\"\\nStatsmodels详细回归结果:\")\n",
    "print(est.summary())\n",
    "\n",
    "print(\"\\n\" + \"=\" * 50)\n",
    "print(\"模型2: 随机森林回归模型\")\n",
    "print(\"=\" * 50)\n",
    "\n",
    "# 模型2: 随机森林回归\n",
    "rf_model = RandomForestRegressor(n_estimators=100, random_state=42)\n",
    "rf_model.fit(X_train, y_train)\n",
    "\n",
    "# 预测\n",
    "y_pred_rf = rf_model.predict(X_test)\n",
    "\n",
    "# 评估\n",
    "r2_rf = r2_score(y_test, y_pred_rf)\n",
    "mse_rf = mean_squared_error(y_test, y_pred_rf)\n",
    "\n",
    "print('特征重要性:')\n",
    "for feature, importance in zip(X.columns, rf_model.feature_importances_):\n",
    "    print(f'{feature}: {importance:.4f}')\n",
    "print(f'R²得分: {r2_rf:.4f}')\n",
    "print(f'均方误差: {mse_rf:.4f}')\n",
    "\n",
    "print(\"\\n\" + \"=\" * 50)\n",
    "print(\"模型3: 支持向量机回归模型\")\n",
    "print(\"=\" * 50)\n",
    "\n",
    "# 模型3: 支持向量机回归\n",
    "svr_model = SVR(kernel='rbf', C=1.0, epsilon=0.1)\n",
    "svr_model.fit(X_train, y_train)\n",
    "\n",
    "# 预测\n",
    "y_pred_svr = svr_model.predict(X_test)\n",
    "\n",
    "# 评估\n",
    "r2_svr = r2_score(y_test, y_pred_svr)\n",
    "mse_svr = mean_squared_error(y_test, y_pred_svr)\n",
    "\n",
    "print(f'R²得分: {r2_svr:.4f}')\n",
    "print(f'均方误差: {mse_svr:.4f}')\n",
    "\n",
    "print(\"\\n\" + \"=\" * 50)\n",
    "print(\"模型性能对比\")\n",
    "print(\"=\" * 50)\n",
    "\n",
    "# 模型性能对比\n",
    "models_comparison = pd.DataFrame({\n",
    "    '模型': ['多元线性回归', '随机森林回归', '支持向量机回归'],\n",
    "    'R²得分': [r2_lr, r2_rf, r2_svr],\n",
    "    '均方误差': [mse_lr, mse_rf, mse_svr]\n",
    "})\n",
    "\n",
    "print(models_comparison)\n",
    "\n",
    "# 可选：添加正则化线性回归模型进行比较\n",
    "print(\"\\n\" + \"=\" * 50)\n",
    "print(\"附加模型: 正则化线性回归\")\n",
    "print(\"=\" * 50)\n",
    "\n",
    "# 岭回归\n",
    "ridge_model = Ridge(alpha=1.0)\n",
    "ridge_model.fit(X_train, y_train)\n",
    "y_pred_ridge = ridge_model.predict(X_test)\n",
    "r2_ridge = r2_score(y_test, y_pred_ridge)\n",
    "\n",
    "# Lasso回归\n",
    "lasso_model = Lasso(alpha=0.1)\n",
    "lasso_model.fit(X_train, y_train)\n",
    "y_pred_lasso = lasso_model.predict(X_test)\n",
    "r2_lasso = r2_score(y_test, y_pred_lasso)\n",
    "\n",
    "print(f'岭回归 R²得分: {r2_ridge:.4f}')\n",
    "print(f'Lasso回归 R²得分: {r2_lasso:.4f}')\n",
    "print('Lasso回归系数:' + str(lasso_model.coef_))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "24da6a50",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
